package com.example.answer_staff;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import com.gaosai.staff.utils.CommonUtils;
import com.gaosai.staff.view.CustomFlowLayout;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
/**
 * 新闻详情
 * @author willis
 *
 */
public class ListJournalismDetailsActivity  extends Activity{
	
	private LayoutInflater mInflater ;
	
	/**取得新闻列表新闻的Item数据**/
	private String getNew ;
	
	private DisplayImageOptions options; 
	
	/**返回按钮**/
	private ImageView news_datil_backbutton ;
	
	/**图片**/
	private ImageView news_detail_imageView ;
	
	/**新闻详情标题**/
	private TextView news_detail_title ;
	
	/**新闻详情时间**/
	private TextView news_detail_time ;
	
	/**新闻详情内容**/
	private TextView news_detail_content ;
	
	/**新闻详情中的标签的流式布局**/
	private CustomFlowLayout news_detail_flowlayout ;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.newsroom_details);
		initView() ;
		initData() ;
		setOnClickListener() ;
	}


	/**初始化View***/
	private void initView() {
		getNew = this.getIntent().getStringExtra("new") ;
		mInflater = LayoutInflater.from(this) ;
		options = new DisplayImageOptions.Builder()
		.showImageOnLoading(R.drawable.ic_launcher)
		.showImageForEmptyUri(R.drawable.ic_launcher)
		.showImageOnFail(R.drawable.ic_launcher).cacheInMemory(true)
		.cacheOnDisc(true).considerExifParams(true)
		.bitmapConfig(Bitmap.Config.RGB_565).build();
		
		news_datil_backbutton = (ImageView) this.findViewById(R.id.news_datil_backbutton) ;
		news_detail_imageView = (ImageView) this.findViewById(R.id.news_detail_imageView) ;
		news_detail_title = (TextView) this.findViewById(R.id.news_detail_title) ;
		news_detail_time = (TextView) this.findViewById(R.id.news_detail_time) ;
		news_detail_content = (TextView) this.findViewById(R.id.news_detail_content) ;
		news_detail_flowlayout = (CustomFlowLayout) this.findViewById(R.id.news_detail_flowlayout) ;
		
	}
	
	/**初始化数据**/
	private void initData() {
		try {
			JSONObject jsonObject = new JSONObject(getNew) ;
			ImageLoader.getInstance().displayImage(jsonObject.optString("pic"), news_detail_imageView,options) ;
			news_detail_title.setText(jsonObject.optString("title")) ;
			news_detail_time.setText(jsonObject.optString("addTime")) ;
			news_detail_content.setText(jsonObject.optString("briefContent")) ;
			JSONArray flowLayoutArray = jsonObject.optJSONArray("TTagses") ;
			
			//把标签内容放进流式布局组件中
			news_detail_flowlayout.removeAllViews() ;
			for (int i = 0; i < flowLayoutArray.length(); i++) {
				TextView tv = (TextView) mInflater.inflate(R.layout.tv,news_detail_flowlayout, false);
				String tagName = flowLayoutArray.optJSONObject(i).optString("name") ;
				//取得缩写名字
				String abbreName = CommonUtils.getAbbreviation(tagName) ;
				tv.setText(abbreName) ;
				CommonUtils.setColor(abbreName,tv,ListJournalismDetailsActivity.this) ;
				news_detail_flowlayout.addView(tv) ;
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
	
	/**设置监听事件**/
	private void setOnClickListener() {
		news_datil_backbutton.setOnClickListener(backOnclickListener) ;
	}

	/**返回按钮的监听事件**/
	private View.OnClickListener backOnclickListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			finish() ;
		}
	};
	
}
